package com.lightkits.wms.domain.saveonhand.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.BdOnhandJournal;
import com.lightkits.common.db.entity.BdOnhandQuantity;
import com.lightkits.common.db.entity.FndEventTypeBase;
import com.lightkits.common.db.entity.event.BdEvent;
import com.lightkits.common.db.entity.event.BdEventRecord;
import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.wms.domain.saveonhand.SaveOnhandExecutor;
import com.lightkits.wms.domain.saveonhand.models.OnhandParams;
import com.lightkits.wms.domain.saveonhand.service.CreateEventDomain;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * SaveEventDomainImpl
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/2/21 0:22
 */
@Service
public class CreateEventDomainImpl implements CreateEventDomain {

    @Override
    public BdEvent createEvent(OnhandParams onhandParams) {
        BdEvent event = new BdEvent();
            FndEventTypeBase eventTypeBase = new FndEventTypeBase()
                    .selectOne(new EntityWrapper<FndEventTypeBase>()
                            .eq(FndEventTypeBase.EVENT_TYPE_CODE, onhandParams.getEventTypeCode()));
            event.setEventTypeId(eventTypeBase.getId());
            event.setEventTypeCode(onhandParams.getEventTypeCode());
            event.setEventRequestId(onhandParams.getEventRequestId());
            event.setOrganizationId(onhandParams.getOrgId());
            event.setWorkerId(onhandParams.getWorkerId());
            event.setWarehouseId(onhandParams.getWarehouseId());
            event.setWmAreaId(onhandParams.getWmAreaId());
            event.setItemId(onhandParams.getItemId());
            event.setRemark(onhandParams.getRemark());
            event.setParentEventId(onhandParams.getParentEventId());
            event.setCalendarDay(new Date());
        return event;
    }

    @Override
    public BdEventRecord createEventRecord(String objectType,
                                           String recordType,
                                           String keyValue,
                                           Long eventId,
                                           Long objectId,
                                           String snapshotRecord) {

        BdEventRecord eventRecord = new BdEventRecord();
            eventRecord.setObjectType(objectType);
            eventRecord.setRecordType(recordType);
            if (recordType.equals("OUT")) {
                keyValue = "-" + keyValue;
            }
            eventRecord.setKeyValue(keyValue);
            eventRecord.setObjectId(objectId);
            eventRecord.setEventId(eventId);
            eventRecord.setSnapshotRecord(snapshotRecord);
        return eventRecord;
    }

    @Override
    public BdOnhandJournal createOnhandJournal(BdOnhandQuantity params,
                                               BdEvent event,
                                               String directFlag ) {
        BdOnhandJournal onhand = new BdOnhandJournal();
            onhand.setOrganizationId(params.getOrganizationId());
            onhand.setItemId(params.getItemId());
            onhand.setUom(params.getUom());
            onhand.setEventId(event.getId());
            onhand.setEventBy(event.getCreatedBy());
            onhand.setEventTime(event.getCreationDate());
            onhand.setEventQty(params.getQuantity());
            onhand.setInventoryQty(params.getQuantity());
            onhand.setDirectFlag(directFlag);
            onhand.setLotNumber(params.getLotNumber());
            onhand.setWarehouseId(params.getWarehouseId());
            onhand.setWmAreaId(params.getWmAreaId());
            onhand.setOwnerType(params.getOwnerType());
            onhand.setOwnerId(params.getOwnerId());
        return onhand;
    }

}
